package sunstar.com.xq.presenter

import cn.bmob.v3.BmobObject
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import sunstar.com.xq.databean.BannerData
import sunstar.com.xq.databean.CardData
import sunstar.com.xq.view.SquareView

/**
 * Created by Sun.Star on 2017/6/9.
 */

class SquarePresenter(var squareView: SquareView){
    var imgList = mutableListOf<String>()
    var textList = mutableListOf<String>()
    fun initData(list: MutableList<CardData>){
         var query = BmobQuery<BannerData>()
        query.setLimit(5)

        query.findObjects(object : FindListener<BannerData>() {
            override fun done(p0: MutableList<BannerData>?, p1: BmobException?) {
                try {
                for (i in 0 .. p0!!.size-1){
                    imgList.add(p0[i].imgUrl)
                    textList.add(p0[i].textName)
                }
                squareView.initBannerData(imgList,textList)
                }catch (e:Exception){}
            }
        })
        getListData(list)
    }
    fun getListData(list: MutableList<CardData>){
        var query = BmobQuery<CardData>()
        query.setLimit(5)
        query.findObjects(object : FindListener<CardData>() {
            override fun done(p0: MutableList<CardData>?, p1: BmobException?) {
                try {
                    list!!.addAll(p0!!)
                    squareView.adapterNotify()
                }catch (e:Exception){}

            }
        })
    }

}
